//
//  XHDisplayMediaViewController.m
//  MessageDisplayExample
//
//  Created by HUAJIE-1 on 14-5-6.
//  Copyright (c) 2014年 曾宪华 开发团队(http://iyilunba.com ) 本人QQ:543413507 本人QQ群（142557668）. All rights reserved.
//

#import "XHDisplayMediaViewController.h"
#import <MediaPlayer/MediaPlayer.h>

#import "UIView+XHRemoteImage.h"
#import "XHConfigurationHelper.h"
#import "CWQIMNetManager.h"
#import "XHMessage.h"
@interface XHDisplayMediaViewController ()

@property (nonatomic, strong) MPMoviePlayerController *moviePlayerController;

@property (nonatomic, weak) UIImageView *photoImageView;

@property (nonatomic,  strong) MBProgressHUD *hud;

@end

@implementation XHDisplayMediaViewController
{
    MBProgressHUD *_hud;
}
- (MPMoviePlayerController *)moviePlayerController {
    if (!_moviePlayerController) {
        _moviePlayerController = [[MPMoviePlayerController alloc] init];
        _moviePlayerController.repeatMode = MPMovieRepeatModeOne;
        //_moviePlayerController.scalingMode = MPMovieScalingModeAspectFill;
        _moviePlayerController.view.frame = self.view.frame;
        [_moviePlayerController setScalingMode:MPMovieScalingModeAspectFit];
        _moviePlayerController.view.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
        [self.view addSubview:_moviePlayerController.view];
    }
    return _moviePlayerController;
}

- (UIImageView *)photoImageView {
    if (!_photoImageView) {
        UIImageView *photoImageView = [[UIImageView alloc] initWithFrame:self.view.frame];
        photoImageView.contentMode = UIViewContentModeScaleAspectFill;
        photoImageView.clipsToBounds = YES;
        [self.view addSubview:photoImageView];
        _photoImageView = photoImageView;
    }
    return _photoImageView;
}

- (void)setMessage:(id<XHMessageModel>)message {
    _message = message;
    defineWeakSelf;
    __block CGFloat f =0;
    [CoreSVP showProgess:f Msg:@"正在获取...." maskType: SVProgressHUDMaskTypeNone];
    self.hud = [[WCPopupLayerManager shareMg] showSimpleInImageView:self.view];
    if ([message messageMediaType] == XHBubbleMessageMediaTypeVideo) {
        self.title = NSLocalizedStringFromTable(@"Video", @"MessageDisplayKitString", @"详细视频");
        XHMessage *me = (XHMessage *)message;
        if (!me.videoPath) {
            [CWQIMNetManager IMGET:@"http://api.weixin.qq.com/cgi-bin/media/get" parameters:me.mediaId fromId:@"123" progress:^(CGFloat percent) {
                
                f = percent;
            } success:^(id responseObject) {
                [CoreSVP dismiss];
                me.videoPath = responseObject;
                NSString *videoPath = [me videoPath];
                NSString *path = [videoPath substringFromIndex:[videoPath length]-34];
                
                NSURL *url = [NSURL fileURLWithPath:[NSHomeDirectory() stringByAppendingFormat:@"%@", path]];
                weakSelf.moviePlayerController.contentURL = url;
                [weakSelf.moviePlayerController play];
                [me updateToDB];
            } failure:^(NSString *msg) {
                [CoreSVP dismiss];
                
                
                
                [weakSelf.navigationController popViewControllerAnimated:YES];
                
                
            }];
        }else {
            NSString *videoPath = [me videoPath];
            NSString *path = [videoPath substringFromIndex:[videoPath length]-34];
            
            NSURL *url = [NSURL fileURLWithPath:[NSHomeDirectory() stringByAppendingFormat:@"%@", path]];
            weakSelf.moviePlayerController.contentURL = url;
            [weakSelf.moviePlayerController play];
        
        }
        
        
    } else if ([message messageMediaType] ==XHBubbleMessageMediaTypePhoto) {
        [CoreSVP dismiss];
        self.title = NSLocalizedStringFromTable(@"Photo", @"MessageDisplayKitString", @"详细照片");
        self.photoImageView.image = message.photo;
        if (message.thumbnailUrl) {
            NSString *placeholderImageName = [[XHConfigurationHelper appearance].messageInputViewStyle objectForKey:kXHMessageTablePlaceholderImageNameKey];
            if (!placeholderImageName) {
                placeholderImageName = @"placeholderImage";
            }
            
            [self.photoImageView setImageWithURL:[NSURL URLWithString:[message thumbnailUrl]] placeholer:[UIImage imageNamed:placeholderImageName]];
        }
    }
}

#pragma mark - Life cycle

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
      [CoreSVP dismiss];
    if ([self.message messageMediaType] == XHBubbleMessageMediaTypeVideo) {
        [self.moviePlayerController stop];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    

   
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [_moviePlayerController stop];
    _moviePlayerController = nil;
    
    _photoImageView = nil;
}

@end
